package com.zretc.service.impl;

import com.zretc.dao.OpLogMapper;
import com.zretc.entity.OpLog;
import com.zretc.service.OpLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 周文涛
 * 2020年8月25日17:01:25
 * 操作日志业务层实现类
 */
@Service
public class OpLogServiceImpl implements OpLogService {

    @Autowired
    private OpLogMapper opLogMapper;

    @Override
    public List<Map<String, Object>> getAll(Map<String, String> condition) {
        if(condition == null){
            condition = new HashMap<>();
        }
        String startTime = condition.get("startTime");
        String endTime = condition.get("endTime");
        if("".equals(startTime)){
            condition.put("startTime",null);
        }
        if("".equals(endTime)){
            condition.put("endTime",null);
        }
        List<OpLog> all = opLogMapper.findAll(condition);
        List<Map<String, Object>> list = new ArrayList<>();
        for(OpLog opLog:all){
            HashMap<String, Object> map = new HashMap<>();
            map.put("id",opLog.getId());
            map.put("addDate",opLog.getAddDate());
            map.put("operate",opLog.getOperate());
            map.put("userId",opLog.getUserInfo().getUserId());
            map.put("userName",opLog.getUserInfo().getUsername());
            map.put("name",opLog.getUserInfo().getName());
            list.add(map);
        }
        return list;
    }

    @Override
    public void save(OpLog opLog) {
        opLogMapper.insert(opLog);
    }
}
